// subingwen - 9.24
#include "skinitem.h"
#include <QPainter>
#include <QMouseEvent>

/*
 * 皮肤窗口缩略图
 * 缩略图大小 - 225*110
 * 外部边框 - 绿色, 2pixel
 * 边框和缩略图间距 - 2pixel
*/
SkinItem::SkinItem(QString path, QWidget *parent) :
    QWidget(parent),
    m_path(path)
{
    setFixedSize(WIDTH, HEIGHT);
}

void SkinItem::setSelected(bool bl)
{
    m_isSelect = bl;
    m_isBorder = bl;
    update();
}

void SkinItem::enterEvent(QEvent *event)
{
    m_isBorder = true;
    update();
}

void SkinItem::leaveEvent(QEvent *event)
{
    m_isBorder = false;
    update();
}

void SkinItem::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPen pen(QBrush(QColor(Qt::green)), 2);
    painter.setPen(pen);
    // 画绿色边框
    if(m_isBorder || m_isSelect)
    {
        painter.drawRect(2, 2, width()-4, height()-4);
        // 画背景图
        painter.drawPixmap(5, 5, width()-10, height()-10, QPixmap(m_path));
    }
    else
    {
        // 画背景图
        painter.drawPixmap(1, 1, width()-2, height()-2, QPixmap(m_path));
    }
    // 选中的图标
    if(m_isSelect)
    {
        QPixmap pixImg(":/images/select.png");
        int x = width() - 8 - 5 - pixImg.width();
        int y = height() - 8 - 5 - pixImg.height();
        painter.drawPixmap(x, y, pixImg);
    }
}

void SkinItem::mousePressEvent(QMouseEvent *event)
{
    // 左键按下
    if(event->button() == Qt::LeftButton)
    {
        m_isSelect = true;
        update();
        emit itemClicked();
    }
}

/********************************************************************************
 *                              华丽的分割线
 *                          Class: SkinItemWidget
 ********************************************************************************/
#include <QHBoxLayout>
#include <QLabel>

SkinItemWidget::SkinItemWidget(QString path, QWidget *parent) :
    QWidget(parent),
    m_path(path)
{
    m_item = new SkinItem(path, parent);
    initSkinItemWidget();
}

SkinItemWidget::SkinItemWidget(QString path, QString name, QString author, QWidget *parent) :
    QWidget(parent),
    m_path(path),
    m_name(name),
    m_author(author)
{
    m_item = new SkinItem(path, parent);
    initSkinItemWidget();
}

void SkinItemWidget::initSkinItemWidget()
{
    QHBoxLayout* hlayout = new QHBoxLayout;
    QLabel* name = new QLabel(m_name);
    QLabel* author = new QLabel("作者: " + m_author);
    name->setStyleSheet("background-color: transparent");
    author->setStyleSheet("background-color: transparent; color: gray");
    hlayout->addWidget(name);
    hlayout->addStretch(0);
    hlayout->addWidget(author);
    hlayout->setContentsMargins(6, 0, 10, 0);

    QVBoxLayout* vlayout = new QVBoxLayout;
    vlayout->addWidget(m_item);
    vlayout->addLayout(hlayout);
    this->setLayout(vlayout);
    this->setFixedSize(vlayout->sizeHint());

    connect(m_item, &SkinItem::itemClicked, this, &SkinItemWidget::itemClicked);
}

QString SkinItemWidget::getImageName()
{
    return m_name;
}

QString SkinItemWidget::getImageAuthor()
{
    return m_author;
}

void SkinItemWidget::setImageInfo(QString name, QString author)
{
    m_name = name;
    m_author = author;
}

void SkinItemWidget::setSelected(bool bl)
{
    m_item->setSelected(bl);
}

